package com.xzy.demo.juc;

/**
 * @author xzy
 * @date 2021/3/21 9:18
 * 说明：
 */
public class SaleTicketDemo00 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(() -> {for (int i = 0; i < 40; i++){ticket.sale();}}, "A").start();
        new Thread(() -> {for (int i = 0; i < 40; i++){ticket.sale();}}, "B").start();
        new Thread(() -> {for (int i = 0; i < 40; i++){ticket.sale();}}, "C").start();
    }
}

class Ticket {
    private int number = 30;

    public void sale() {
        synchronized (this) {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + " 卖出票 " + (number--) + " 剩余 " + number);
            }
        }
    }
}